package com.neusoft.javafeatures9.nionetty.userguide4x.time;

import com.neusoft.javafeatures9.nionetty.userguide4x.protobuf.ProtoBufWrapper;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelFutureListener;

import java.util.Date;


public class TimeClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ProtoBufWrapper.MyMessageVo myMessageVo = ProtoBufWrapper.
                MyMessageVo.newBuilder()
                .setMsgName("time")
                .setCreateDate(1)
                .setMsgType(ProtoBufWrapper.MyMessageVo.MessageTypeEnum.SCHOOL)
                .setSchool(ProtoBufWrapper.School.newBuilder().setSchoolName("学校").build())
                .build();

        ByteBuf m = (ByteBuf) msg; // (1)
        try {
            long currentTimeMillis = (m.readUnsignedInt() - 2208988800L) * 1000L;
            System.out.println(new Date(currentTimeMillis));
            ctx.close();
        } finally {
            m.release();
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}
